﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Web;

using ChatForum.Domain;
using ChatForum.REST.ServiceContract;
using ChatForum.DataContract;

namespace ChatForum.Service.REST
{
    public class AccountService : ServiceBase, IAccountService
    {
        public AccountService(BusinessLogic logic)
            : base(logic)
        { }

        public void CreateUser(UserDTO user)
        {
            try
            {
                if (user == null)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    return;
                }
                logic.RegisterMe(user.Convert());
                Uri locationUri = new Uri(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.OriginalString + "/" + user.UserId);
                WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(locationUri);
            }
            catch (Exception ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError);
            }
        }
        public void DeleteAllUser()
        {
            try
            {
                throw new NotImplementedException();
            }
            catch (NotImplementedException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.NotImplemented);
            }
        }
        public void DeleteUser(string userId)
        {
            try
            {
                throw new NotImplementedException();
            }
            catch (NotImplementedException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.NotImplemented);
            }
        }
        public IEnumerable<UserDTO> GetAllUser()
        {
            try
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                return logic.GetAllUser().Select(u => new UserDTO(u));
            }
            catch (Exception ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError);
            }
        }
        public UserDTO GetUser(string userId)
        {
            try
            {
                User user = logic.GetUser(userId);
                if (user == null)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NotFound;
                    return null;
                }
                else
                    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                return new UserDTO(user);
            }
            catch (Exception ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError);
            }
        }
        public void ModifyAllUser(IEnumerable<UserDTO> userCollection)
        {
            try
            {
                if (userCollection == null || userCollection.Count() == 0)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    return;
                }
                foreach (UserDTO user in userCollection)
                {
                    logic.ModifyAccount(user.Convert());
                }
            }
            catch (NotImplementedException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError);
            }
        }
        public void ModifyUser(string userId, UserDTO user)
        {
            try
            {
                if (user == null)
                {
                    WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    return;
                }
                logic.ModifyAccount(user.Convert());
            }
            catch (NotImplementedException ex)
            {
                throw new WebFaultException<string>(ex.Message, System.Net.HttpStatusCode.InternalServerError);
            }
        }
    }
}
